// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/nam/ntr.cpp $
// $Rev: 100 $
// $Author: akio.miyoshi $
// $Date:: 2010-05-24 03:06:14 +0000#$

#include "ntr.h"

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

//NetworkThreadRequest::NetworkThreadRequest(QObject *parent)
NetworkThreadRequest::NetworkThreadRequest(QNetworkAccessManager::Operation a_op, QNetworkRequest a_request, QByteArray a_postData)
    : QThread(0)
    , m_op(a_op)
    , m_request(a_request)
    , m_postData(a_postData)
{
}

void NetworkThreadRequest::run()
{
    ////qDebug() << "NetworkThreadRequest::run(1)";
    m_response.clear();
    m_nam = new NetworkAccessManager(this, 0);
    if (m_op == QNetworkAccessManager::GetOperation)
    {
        m_reply = this->m_nam->get(m_request);
    }
    this->QThread::exec();
    ////qDebug() << "NetworkThreadRequest::run(2)";
    ////qDebug() << "[NetworkThreadRequest::run()]" << m_response;
    delete m_reply;
    delete m_nam;
}

void NetworkThreadRequest::replyFinished()
{
    m_response.clear();
    QList<QByteArray> headerList = m_reply->rawHeaderList();
    if(headerList.contains("Location"))
    {
        QByteArray v_locationRaw = m_reply->rawHeader("Location");
        qDebug() << "[v_locationRaw]" << v_locationRaw;
#if 0x0
        QString v_locationStr = QUrl::fromPercentEncoding(v_locationRaw);
        m_response.append(v_locationStr.toLatin1());
#else
        m_response.append(v_locationRaw);
#endif
    }
    else
    {
        QByteArray bytes = m_reply->readAll();
        m_response.append(bytes);
    }
    this->exit(0);
}


QByteArray NetworkThreadRequest::doGet2(QString url)
{
    QUrl v_url;
    v_url.setUrl(url);
    return NetworkThreadRequest::doGet2(v_url);
}

QByteArray NetworkThreadRequest::doGet2(QUrl url)
{
    QNetworkRequest v_request;
    v_request.setUrl(url);
    return NetworkThreadRequest::doGet2(v_request);
}

QByteArray NetworkThreadRequest::doGet2(QNetworkRequest request)
{
    qDebug() << "NetworkThreadRequest::doGet2()" << request.url().toString();
    QByteArray v_empty;
    NetworkThreadRequest *v_ntr = new NetworkThreadRequest(QNetworkAccessManager::GetOperation, request, v_empty);
    v_ntr->start();
    v_ntr->wait();
    QByteArray v_response = v_ntr->m_response;
    delete v_ntr;
    return v_response;
}

QList<QByteArray> NetworkThreadRequest::doGet2(QList<QNetworkRequest> a_requestList)
{
    for(int i=0; i<a_requestList.size(); i++)
    {
        qDebug() << "NetworkThreadRequest::doGet2(QList)" << a_requestList.at(i).url().toString();
    }
    QByteArray v_empty;
    QList<NetworkThreadRequest *> v_ntrList;
    for(int i=0; i<a_requestList.size(); i++)
    {
        QNetworkRequest v_request = a_requestList.at(i);
        NetworkThreadRequest *v_ntr = new NetworkThreadRequest(QNetworkAccessManager::GetOperation, v_request, v_empty);
        v_ntrList.append(v_ntr);
    }
    for(int i=0; i<v_ntrList.size(); i++)
    {
        NetworkThreadRequest *v_ntr = v_ntrList.at(i);
        v_ntr->start();
    }
    QList<QByteArray> v_responseList;
    for(int i=0; i<v_ntrList.size(); i++)
    {
        NetworkThreadRequest *v_ntr = v_ntrList.at(i);
        v_ntr->wait();
        v_responseList.append(v_ntr->m_response);
        delete v_ntr;
    }
    return v_responseList;
}
